package com.qk.management.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.qk.common.PageResult;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.entity.Course;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public PageResult<Course> page(String name, Integer sucject, Integer target, Integer page, Integer pageSize) {
        Integer total = courseMapper.count(name,sucject,target);
        Integer offset = (page-1)*pageSize;
        List<Course> rows = courseMapper.selectPage(name,sucject,target,offset,pageSize);
        return PageResult.<Course>builder()
                .total(total)
                .rows(rows)
                .build();
    }

    @Override
    public void add(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.insert(course);
    }

    @Override
    public Course getById(Integer id) {
        Course course =courseMapper.selectById(id);
        return course;
    }

    @Override
    public void update(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);
    }

    @Override
    public void deleteById(Integer id) {
        courseMapper.deleteById(id);
    }

    @Override
    public List<Course> listBySubject(Integer subject) {
        if (Objects.isNull(subject)){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        return courseMapper.selectBySubject(subject);
    }

    @Override
    public List<Course> getAllCourses() {
       return courseMapper.getAllCourses();
    }
}
